/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: SignaleEventReceived.java
 * Author:   cakin
 * Date:     2020/4/23
 * Description: signaleEventReceived方法触发信号事件
 */
package com.syc.activiti.chapter9;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

/**
 * @className: SignaleEventReceived
 * @description: signaleEventReceived方法触发信号事件
 * @date: 2020/4/23
 * @author: cakin
 */
public class SignaleEventReceived {

    public static void main( String[] args ) {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 得到流程存储服务实例
        RepositoryService repositoryService = engine.getRepositoryService();
        // 得到运行时服务组件
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程描述文件
        repositoryService.createDeployment()
                .addClasspathResource("bpmn/chapter9/signalEventReceived.bpmn").deploy();
        // 开始流程
        ProcessInstance pi = runtimeService
                .startProcessInstanceByKey("testProcess");
        // 查询执行流
        Execution exe = runtimeService.createExecutionQuery().activityId("usertask1").singleResult();
        System.out.println("当前执行流：" + exe.getId() + " 当前节点：" + exe.getActivityId());
        // 触发receiveTask
        runtimeService.trigger(exe.getId());
        // 查询当前节点
        exe = runtimeService.createExecutionQuery().activityId("signalCatchEvent").singleResult();
        System.out.println("当前执行流：" + exe.getId() + " 调用trigger方法后当前节点：" + exe.getActivityId());
        // 发送信号给事件，流程结束
        runtimeService.signalEventReceived("testSignal");
        List exes = runtimeService.createExecutionQuery()
                .processInstanceId(pi.getId()).list();
        System.out.println(" 触发Catch事件后，执行流数量：" + exes.size());
    }
}
